package com.ateam.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ateam.biz.CompaniesBiz;
import com.ateam.biz.SharesBiz;
import com.ateam.biz.UsersBiz;
import com.ateam.pojo.Companies;
import com.ateam.pojo.Shares;
import com.ateam.pojo.Users;
import com.ateam.util.DateFactory;
import com.ateam.util.PojoFactory;

@Controller
public class UsersController {
	@Resource(name = "usersBiz")
	private UsersBiz usersBiz;

	@Resource(name = "companiesBiz")
	private CompaniesBiz companiesBiz;

	@Resource(name = "sharesBiz")
	private SharesBiz sharesBiz;

	// 用户登录模块，判断是否有所属公司，若有，跳至home，否则跳初始选择页面，登录失败则停留在index
	@RequestMapping("login.do")
	public String userLogin(Users u, HttpSession session, HttpServletRequest request) {
		System.out.println("尝试登录的用户是" + u.toString());
		Users currentUser = usersBiz.loginUser(u);
		if (currentUser != null) {
			System.out.println("当前用户是" + currentUser.toString());
			Companies currentCompany = companiesBiz.getCompanyByUser(currentUser);
			session.setAttribute("currentUser", currentUser);
			if (currentCompany.getCompanyid() != 1) {
				System.out.println("当前的企业是" + currentCompany.toString());
				session.setAttribute("currentCompany", currentCompany);
				return "home";
			} else
				return "loginsuccess";
		} else
			return "index";
	}

	// 用户注册
	@RequestMapping("register.do")
	public String userRegister(Users u, HttpSession session, HttpServletRequest request) {
		System.out.println("尝试注册的用户是" + u.toString());
		if (0 != usersBiz.registerUser(u)) {
			return "index";
		} else
			return "register";
	}

	// 获取所有用户
	@ResponseBody
	@RequestMapping("getallusers.do")
	public List<Users> getAllUsers(HttpSession session, HttpServletRequest request) {
		// 后期修改代码，当前用户和公司都用工厂类获得
		if (session.getAttribute("currentCompany") != null) {
			Companies currentCompany = (Companies) session.getAttribute("currentCompany");
			System.out.println("getAllusers中当前公司的名称是" + currentCompany.getCompanyname());
			List<Users> users = usersBiz.getUsersByCompanyid(currentCompany.getCompanyid());
			return users;
		} else
			return null;
	}

	// 公司创建者获取申请加入公司的人
	@RequestMapping("getapplyers.do")
	public String getapplyers(HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Companies currentCompany = (Companies) session.getAttribute("currentCompany");
				List<Users> applyerlist = usersBiz.getCompanyApplyer(currentCompany.getCompanyid());
				request.setAttribute("applyerlist", applyerlist);
				return "applylist";
			} else
				return "home";
		} else
			return "index";
	}

	// 前往查看同事的页面
	@RequestMapping("getcolleagues.do")
	public String getcolleagues(HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Companies currentCompany = (Companies) session.getAttribute("currentCompany");
				List<Users> colleagues = usersBiz.getUsersByCompanyid(currentCompany.getCompanyid());
				request.setAttribute("colleagues", colleagues);
				return "colleagues";
			} else
				return "home";
		} else
			return "index";
	}

	// 更改用户信息
	@ResponseBody
	@RequestMapping("changeuserinfo.do")
	public boolean changeuserinfo(Users changeduser, HttpSession session, HttpServletRequest request) {
		System.out.println("修改的用户信息是" + changeduser.toString());

		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Users currentUser = (Users) session.getAttribute("currentUser");
				String username = currentUser.getUsername();
				if (0 != usersBiz.updateUserInfo(changeduser, currentUser)) {
					Shares share = PojoFactory.getShare();
					share.setContent("于" + DateFactory.getDateToStr(new Date()) + "修改了个人信息。原用户名是" + username);
					sharesBiz.addShareByUser(share, currentUser, 1, 1);
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}

	// 登出
	@RequestMapping("logout.do")
	public String userLogout(HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				session.removeAttribute("currentUser");
				session.removeAttribute("currentCompany");
				return "index";
			} else
				return "index";
		} else
			return "index";
	}
}
